<?php
namespace libs\qoy;
require_once "Autoload.php";
use libs\qoy\Exception\ExceptionCode;
use libs\qoy\Exception\ExceptionBadRequest;
/**
 * qoy框架入口
 * 
 */

class Enter {
   
    /**
     * 接口控制器
     * @var BaseController
     */
    protected  $controller;
    /**
     *
     * @var DI 
     */
    private $di;


    public function __construct() {
        $this->di=DI::instance();
    }
    
    public function rum() {
        try {
            
              $rs= $this->di->response;
              Request::init();
              $this->di->log->info("处理开始", Request::geturl());
              
              $appconfig=$this->di->config;
               
              Request::rum($appconfig->get("app.app_path", "app"),$appconfig->get("app.default_action", "Index"),$appconfig->get("app.default_Controllerm", "/Index/index"));
              $m=Request::getController();
              $this->controller=new $m();
              $action=Request::getAction();
              
              if(method_exists($this->controller,$action)){
                 $this->controller->init();
                 $data = call_user_func(array($this->controller, $action));
                 $rs->setData($data); 
              }else{
                throw new ExceptionBadRequest(ExceptionBadRequest::ACTION);
              }
             
        }catch (ExceptionCode $exc) {
            
            $this->di->log->error($exc->getMessage().$exc->getCode());
            $rs->setRet($exc->getCode());
            $rs->setMsg($exc->getMessage());
            $rs->setData($exc->getErrerData()); 
        }
        $rs->output();
        $this->di->log->info("================================处理结束================================");
    }
}
